package com.jeesite.modules.wx.miniapp.controller;

import com.google.common.collect.Maps;
import com.jeesite.common.config.Global;
import com.jeesite.common.lang.StringUtils;
import com.jeesite.common.web.BaseController;
import com.jeesite.modules.bis.entity.BisDispatch;
import com.jeesite.modules.bis.entity.BisUser;
import com.jeesite.modules.bis.service.BisDispatchService;
import com.jeesite.modules.bis.service.BisUserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Calendar;
import java.util.List;
import java.util.Map;

/**
 * 获取业务数据
 */
@RestController
@RequestMapping("/wx/dispatch/{appid}")
public class WxBisDispatchController extends BaseController {
    @Autowired
    private BisDispatchService bisDispatchService;

    @Autowired
    private BisUserService bisUserService;

    /**
     * 列表
     *
     * @param sessionKey 标识
     * @param type       类型：1: 接单-待接单 2: 接单-已接单 3: 接单-已拒绝  4: 抢单-待接单 5: 抢单-已接单 6: 抢单-已拒绝
     * @return 列表数据
     */
    @PostMapping("/listData")
    public String listData(String sessionKey, String type) {
        if (StringUtils.isBlank(sessionKey) || StringUtils.isBlank(type)) {
            return renderResult(Global.FALSE, "非法访问");
        }
        BisUser bisUser = bisUserService.getBySessionKey(sessionKey);
        if (bisUser == null) {
            return renderResult(Global.FALSE, "没有找到用户");
        }
        int overtime = Integer.parseInt(Global.getConfig("bis.dispatch.overtime.minutes", "10"));
        BisDispatch bisDispatch = new BisDispatch();
        Calendar c;
        switch (type) {
            case "1":
                bisDispatch.setDipatchType(1L);
                bisDispatch.setOrderStatus("1");
                bisDispatch.setAssignorId(bisUser.getId());
                c = Calendar.getInstance();
                c.add(Calendar.MINUTE, -overtime);
                bisDispatch.setOrderTime(c.getTime());
                break;
            case "2":
                bisDispatch.setDipatchType(1L);
                bisDispatch.setOrderStatus("2");
                bisDispatch.setAssignorId(bisUser.getId());
                break;
            case "3":
                bisDispatch.setDipatchType(1L);
                bisDispatch.setOrderStatus("4");
                bisDispatch.setAssignorId(bisUser.getId());
                break;
            case "4":
                bisDispatch.setDipatchType(2L);
                bisDispatch.setOrderStatus("1");
                c = Calendar.getInstance();
                c.add(Calendar.MINUTE, -overtime);
                bisDispatch.setOrderTime(c.getTime());
                break;
            case "5":
                bisDispatch.setDipatchType(2L);
                bisDispatch.setOrderStatus("2");
                bisDispatch.setAssignorId(bisUser.getId());
                break;
        }
        List<BisDispatch> list = bisDispatchService.findListByType(bisDispatch);
        return renderResult(Global.TRUE, "成功", list);
    }

    /**
     * 所有列表 TODO 待分页
     *
     * @param sessionKey 标识
     * @param searchStr  查询
     * @return 列表数据
     */
    @PostMapping("/allListData")
    public String allListData(String sessionKey, String searchStr) {
        if (StringUtils.isBlank(sessionKey)) {
            return renderResult(Global.FALSE, "非法访问");
        }
        BisUser bisUser = bisUserService.getBySessionKey(sessionKey);
        if (bisUser == null) {
            return renderResult(Global.FALSE, "没有找到用户");
        }
        int overtime = Integer.parseInt(Global.getConfig("bis.dispatch.overtime.minutes", "10"));
        BisDispatch bisDispatch = new BisDispatch();
        bisDispatch.setAssignorId(bisUser.getId());
        bisDispatch.setRemarks(searchStr);
        Calendar c = Calendar.getInstance();
        c.add(Calendar.MINUTE, -overtime);
        bisDispatch.setOrderTime(c.getTime());
        List<BisDispatch> list = bisDispatchService.findListBySearch(bisDispatch);
        return renderResult(Global.TRUE, "成功", list);
    }

    /**
     * 操作订单，完成，拒接，接单
     *
     * @param sessionKey
     * @param id
     * @param type
     * @return
     */
    @PostMapping("/doOperation")
    public synchronized String doOperation(String sessionKey, String id, String type) {
        if (StringUtils.isBlank(sessionKey) || StringUtils.isBlank(type)) {
            return renderResult(Global.FALSE, "非法访问");
        }
        BisUser bisUser = bisUserService.getBySessionKey(sessionKey);
        if (bisUser == null) {
            return renderResult(Global.FALSE, "没有找到用户");
        }
        BisDispatch bisDispatch = bisDispatchService.get(id);
        if (bisDispatch == null) {
            return renderResult(Global.FALSE, "订单数据不存在");
        }
        if (("1".equals(type) || "2".equals(type)) && !StringUtils.equals("1", bisDispatch.getOrderStatus())) {
            return renderResult(Global.FALSE, "订单已经被抢走了");
        }
        String msg = "成功";
        switch (type) {
            case "1": //接单
                bisDispatch.setOrderStatus("2");
                msg = "接单成功";
                break;
            case "2": //拒绝
                bisDispatch.setOrderStatus("4");
                msg = "订单已拒绝";
                break;
            case "3": //完成
                bisDispatch.setOrderStatus("3");
                msg = "订单已完成";
                break;
        }
        bisDispatchService.updateOrderStatus(bisDispatch);
        if ("1".equals(type) && StringUtils.isBlank(bisDispatch.getAssignorId())) {
            bisDispatch.setAssignorId(bisUser.getId());
            bisDispatch.setAssignorName(bisUser.getName() + " " + bisUser.getMobilePhone());
            bisDispatchService.updateAssignor(bisDispatch);
        }
        return renderResult(Global.TRUE, msg);
    }

    @PostMapping("/getCount")
    public String getCount(String sessionKey) {
        if (StringUtils.isBlank(sessionKey)) {
            return renderResult(Global.FALSE, "非法访问");
        }
        BisUser bisUser = bisUserService.getBySessionKey(sessionKey);
        if (bisUser == null) {
            return renderResult(Global.FALSE, "没有找到用户");
        }
        int status0 = bisDispatchService.getAllCountByStatus("2", bisUser.getId());
        int status1 = bisDispatchService.getAllCountByStatus("3", bisUser.getId());
        int status2 = bisDispatchService.getAllCountByStatus("4", bisUser.getId());
        Map<String, Integer> countMap = Maps.newHashMap();
        countMap.put("status0", status0);
        countMap.put("status1", status1);
        countMap.put("status2", status2);
        return renderResult(Global.TRUE, "成功", countMap);
    }

    @PostMapping("/getNewCount")
    public String getNewCount(String sessionKey) {
        if (StringUtils.isBlank(sessionKey)) {
            return renderResult(Global.FALSE, "非法访问");
        }
        BisUser bisUser = bisUserService.getBySessionKey(sessionKey);
        if (bisUser == null) {
            return renderResult(Global.FALSE, "没有找到用户");
        }

        int status0 = bisDispatchService.getCountByStatus("1", bisUser.getId());
        int status1 = bisDispatchService.getCountByStatus("1", null);
        Map<String, Integer> countMap = Maps.newHashMap();
        countMap.put("status0", status0);
        countMap.put("status1", status1);
        return renderResult(Global.TRUE, "成功", countMap);
    }

    @PostMapping("/getDetail")
    public String getDetail(String sessionKey, String id) {
        if (StringUtils.isBlank(sessionKey)) {
            return renderResult(Global.FALSE, "非法访问");
        }
        BisUser bisUser = bisUserService.getBySessionKey(sessionKey);
        if (bisUser == null) {
            return renderResult(Global.FALSE, "没有找到用户");
        }
        BisDispatch bisDispatch = bisDispatchService.get(id);
        return renderResult(Global.TRUE, "成功", bisDispatch);
    }

}
